package com.jianbo.proxy.util;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class IoUtil {

    /**
	 * 默认缓存大小 8192
	 */
	public static final int DEFAULT_BUFFER_SIZE = 2 << 12;
    
    /**
     * IO操作中共同的关闭方法
     *
     * @param closeable 可关闭工具.
     * @createTime 2014年12月14日 下午7:50:56
     */
    public static void closeIo(AutoCloseable closeable) {
        if (null != closeable) {
            try {
                closeable.close();
            } catch (Exception ignored) {
            }
        }
    }
    
    public static byte[] read(InputStream in) throws IOException {
        // return IoReadUtil.toByteArray(in, "utf-8");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            while(true) {
            final int available = in.available();
                if(available == 0) {
                    break;
                }
                bos.write(in.read());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            in.close();
        }
        return bos.toByteArray();
    }
    
    /**
     * 强制读取多少字节.
     * @param inStream
     * @param length
     * @return
     * @throws IOException
     */
    public static byte[] readToByteBufferByLength(InputStream inStream, int length) throws IOException {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        if (length > 0) {
            int bufferSize = 0x2000;
            byte[] buffer = new byte[bufferSize];
            int read;
            int remaining = length;

            while (remaining > 0) {
                if (remaining < bufferSize) {
                    buffer = new byte[remaining];
                }
                read = inStream.read(buffer);
                if (-1 == read) {
                    break;
                }
                remaining -= read;
                outStream.write(buffer, 0, read);
            }
        }
        return outStream.size() > 0 ? outStream.toByteArray() : null;
    }

    /**
	 * 拷贝流，拷贝后不关闭流
	 *
	 * @param in             输入流
	 * @param out            输出流
	 * @param bufferSize     缓存大小
	 * @param streamProgress 进度条
	 * @return 传输的byte数
	 * @throws IORuntimeException IO异常
	 */
	public static long copy(InputStream in, OutputStream out, int bufferSize) throws RuntimeException {
		if (bufferSize <= 0) {
			bufferSize = DEFAULT_BUFFER_SIZE;
		}

		byte[] buffer = new byte[bufferSize];
		long size = 0;
		try {
			for (int readSize; (readSize = in.read(buffer)) != -1; ) {
				out.write(buffer, 0, readSize);
				size += readSize;
				out.flush();
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return size;
	}

    /**
	 * 拷贝流，使用默认Buffer大小，拷贝后不关闭流
	 *
	 * @param in  输入流
	 * @param out 输出流
	 * @return 传输的byte数
	 * @throws IORuntimeException IO异常
	 */
	public static long copy(InputStream in, OutputStream out) throws RuntimeException {
		return copy(in, out, DEFAULT_BUFFER_SIZE);
	}

    public static BufferedInputStream toBuffered(InputStream in) {
		return (in instanceof BufferedInputStream) ? (BufferedInputStream) in : new BufferedInputStream(in);
	}
}

